package org.csu.measure.controller;

import org.csu.measure.Analyze.LexicalAnalyzer;

import org.csu.measure.service.CodeService;
import org.csu.measure.domain.CodeCount;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

@Controller
@RequestMapping("/size")
@SessionAttributes(value = {"size"})
public class SizeController {


    //跳转到CK界面
    @GetMapping("/goToSize")
    public String signonForm(Model model){
        return "size";
    }

    //跳转到CK界面
    @PostMapping("/getCodeCount")
    public String getCodeCount(Model model, HttpSession session, @RequestParam("code")String code) throws IOException {

        CodeService codeService = new CodeService();
        String dir = "test.java";
        File file = new File(dir);
        //如果文件不存在，创建文件
        if (!file.exists())
            file.createNewFile();
        //创建FileWriter对象
        FileWriter writer = new FileWriter(file);
        //向文件中写入内容
        writer.write(code);
        writer.flush();
        writer.close();

        CodeCount codeCount = codeService.getCodeCount(dir);
        int circle = LexicalAnalyzer.getCircleCount();

        model.addAttribute("circle", circle);
        model.addAttribute("codeCount", codeCount);
        model.addAttribute("code", code);

        return "size";
    }
}
